package com.longrise.android.byjk.plugins.tabthird.invoice;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.byjk.R;
import com.longrise.android.byjk.widget.view.BBswipeRefreshLayout;
import com.longrise.common.base.BaseActivity2;
import com.longrise.common.utils.AppUtil;
import com.longrise.common.utils.PrintLog;

/**
 * Created by Administrator on 2017/11/21.
 */

public class ConsumeDetailActivity extends BaseActivity2<ConsumeDetailPresenter> implements
        ConsumeDetailContract.View,SwipeRefreshLayout.OnRefreshListener, View.OnClickListener {

    public static final String INTENT_EXTRA_PARAMS = "detailorderid";
    private RecyclerView consume_detail_irv;
    private BBswipeRefreshLayout mSwipeRefreshLayout;
    private TextView tv_course_mount;
    private TextView tv_course_discount;
    private RelativeLayout consume_detail_more;
    private ConsumeDetailRcvAdapter mAdapter;
    //订单id
    private String mOrderId;

    private int mPageNum = 1;   //第几页
    private int mSinglepageNum = 10;  //单次请求获取的item数量
    private boolean isFirstLoaded = false; //页面第一次有没有重新加载过数据
    private String state = "0";

    public static final String ORDER_ID = "orderid";
    private View mfootView;
    private View mheaderView;

    @Override
    public int getContentViewId(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_consume_detail;
    }

    @Override
    public void initView() {
        consume_detail_irv= (RecyclerView) findViewById(R.id.consume_detail_irv);
        mSwipeRefreshLayout = (BBswipeRefreshLayout) findViewById(R.id.consume_detail_swipelayout);

        getExtraData();
        initRec();
        PrintLog.e("ConsumeDetail","mPresenter=:"+mPresenter.toString());
        mPresenter.initData(mOrderId,mPageNum,mSinglepageNum,isFirstLoaded,state);
    }

    @Override
    public void onToolbarBackClick() {
        finish();
    }

    public void initRec() {
        mheaderView = View.inflate(mContext, R.layout.item_consume_detail_header, null);
        mfootView = View.inflate(mContext, R.layout.item_consume_detail_footer, null);
        mSwipeRefreshLayout.setOnRefreshListener(this);
        mAdapter = new ConsumeDetailRcvAdapter(true);
        mAdapter.addHeaderView(mheaderView);
        mAdapter.addFooterView(mfootView);
        consume_detail_more = (RelativeLayout) mheaderView.findViewById(R.id.consume_detail_item2_more_rl1);
        consume_detail_more.setOnClickListener(this);
        consume_detail_irv.setLayoutManager(new LinearLayoutManager(mContext));
        consume_detail_irv.setAdapter(mAdapter);
        setToolbarTitle(AppUtil.getString(R.string.consume_detail));
    }

    private void getExtraData() {
        Intent extraIntent = getIntent();
        mOrderId = extraIntent.getStringExtra(INTENT_EXTRA_PARAMS);
        PrintLog.e("ConsumeDetail","mOrderId=:"+mOrderId);
    }


    @Override
    public void onRefresh() {
        mPresenter.refreshAll();
    }

    @Override
    public void refreshComplete() {
        mSwipeRefreshLayout.setRefreshing(false);
        consume_detail_irv.smoothScrollToPosition(0);
    }

    @Override
    public void setRefreshing(boolean isRefreshing) {
        mSwipeRefreshLayout.setRefreshing(isRefreshing);
    }

    //刷新我的课程
    @Override
    public void refreshConsumeDetail(EntityBean bean) {
        tv_course_mount = (TextView) mfootView.findViewById(R.id.tv_course_mount);  //课程总价
        tv_course_discount = (TextView) mfootView.findViewById(R.id.tv_course_discount);  //折后价格
        int count = bean.getInt("pnum");
        String total_fee = bean.getString("total_fee");
        String payfee = bean.getString("payfee");
        EntityBean[]  beans = (EntityBean[]) bean.get("subject");
        PrintLog.e("ConsumeDetail","beans=:"+beans.toString());
        if(count >3){
            consume_detail_more.setVisibility(View.VISIBLE);
        }else {
            consume_detail_more.setVisibility(View.GONE);
        }
        tv_course_mount.setText("¥" + total_fee);
        tv_course_discount.setText("¥" + payfee);
        mAdapter.refreshData(beans);
    }

    @Override
    public void loadMoreFail() {
        mPageNum = mPageNum -1;

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.consume_detail_item2_more_rl1:
                Intent consumeDetailIntent = new Intent(mContext, ConsumeDetailActivity2.class);
                consumeDetailIntent.putExtra(ORDER_ID, mOrderId);  //订单id
                startActivity(consumeDetailIntent);
                break;
        }
    }
}
